//
//  MyNavigationController.m
//  RulerRangeFinder
//
//  Created by ytkjs2 on 2019/5/24.
//  Copyright © 2019 ytk. All rights reserved.
//

#import "MyNavigationController.h"
#import "DownloadViewController.h"

@interface MyNavigationController ()

@end

@implementation MyNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [[UINavigationBar appearance] setShadowImage:[UIImage new]];
//    if (IS_IPHONE_X)
//    {
//        [[UINavigationBar appearance]setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//    }
//    else
//        [[UINavigationBar appearance]setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//
//    [[UINavigationBar appearance] setShadowImage:[UIImage new]];
//    [[UINavigationBar appearance]setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"icon_navigationIMG"] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
//    [[UINavigationBar appearance] setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    
    
    
//    self.navigationBar.backgroundColor = APP_THEME_COLOR;
    
//    [self.navigationBar setBarTintColor:[UIColor clearColor]];
//    [self.navigationBar setTintColor:[UIColor clearColor]];
//    [self.navigationBar setBackgroundColor:[UIColor clearColor]];
    [self.navigationBar setShadowImage:[UIImage new]];
    [self.navigationBar setBackgroundImage:[self imageWithColor:APP_THEME_COLOR] forBarMetrics:UIBarMetricsDefault];//RGB(20, 49, 125, 1)
//    [self.navigationBar setShadowImage:[UIImage new]];
//    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//
    NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil];
    [[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
//    [UINavigationBar appearance].translucent = NO;
//    [self.navigationBar setShadowImage:[self imageWithColor:[UIColor whiteColor]]];
    
//    self.extendedLayoutIncludesOpaqueBars = YES;
    
//    self.interactivePopGestureRecognizer.enabled = NO;
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    if (self.viewControllers.count>1) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setBackgroundImage:[UIImage imageNamed:@"clearColorIMG"] forState:UIControlStateNormal];
        backButton.frame = CGRectMake(0, 0, 24, 44);

        UIImageView *backIMG = [[UIImageView alloc]init];
        [backIMG setFrame:CGRectMake(0, 10, 12, 20)];
        if ([viewController isKindOfClass:[DownloadViewController class]]) {
            NSLog(@"");
            DownloadViewController * downloadVC = [DownloadViewController new];
            downloadVC = (DownloadViewController *)viewController;
            if (downloadVC.isLongPic)
            {
                [backIMG setImage:IMG_BACK1];
                [backIMG setFrame:CGRectMake(0, 10, 20, 20)];
            }
            else
                [backIMG setImage:IMG_BACK];
        }
        else
            [backIMG setImage:IMG_BACK];
        
        [backButton addSubview:backIMG];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    }
}

-(void)back{
    [self popViewControllerAnimated:YES];
}

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
